<pre class='metadata'>
Title: CSS Scoping Module Level 1
Level: 1
Shortname: css-scoping
Group: CSSWG
Status: ED
Work Status: Exploring
TR: https://www.w3.org/TR/css-scoping-1/
ED: https://drafts.csswg.org/css-scoping/
Previous Version: https://www.w3.org/TR/2014/WD-css-scoping-1-20140403/
Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/, w3cid 42199
Abstract: This specification defines scoping/encapsulation mechanisms for CSS, focusing on the Shadow DOM scoping mechanism.
Ignored Terms: inherit, slot, custom elements, stylesheets
Ignored Vars: root elements
WPT Path Prefix: css/css-scoping/
WPT Display: closed
</pre>

<pre class="link-defaults">
spec:dom; type:dfn;
	text:host
	text:shadow root; for:/
	text:root; for:tree
	text:find slottables
	text:find flattened slottables
	text:element; for:/
spec:html; type:element;
	text:style
spec:selectors-4; type:dfn;
	text: static profile
	text: dynamic profile
spec:css-pseudo-4; type:selector;
	text:::before
	text:::after
spec:cascade-4; type:dfn; text: inherit
spec:css-fonts-4; type:property; text:font-family
spec:selectors-4; type:dfn; text:type selector
spec:dom; type:dfn; text:descendant
spec:dom; type:dfn; text:children
spec:dom; type:dfn; text:child
spec:css-color-5; type:function; text:color()
</pre>

<!--

<h2 id="intro">
Introduction</h2>

	... -->



<!--
████████  ████████ ████████    ███    ██     ██ ██       ████████
██     ██ ██       ██         ██ ██   ██     ██ ██          ██
██     ██ ██       ██        ██   ██  ██     ██ ██          ██
██     ██ ██████   ██████   ██     ██ ██     ██ ██          ██
██     ██ ██       ██       █████████ ██     ██ ██          ██
██     ██ ██       ██       ██     ██ ██     ██ ██          ██
████████  ████████ ██       ██     ██  ███████  ████████    ██
-->

<h2 id='default-element-styles'>
Default Styles for Custom Elements</h2>

	Advisement: This section is <strong>experimental</strong>,
	and is under active discussion.
	Do not implement without consulting the CSSWG.

	When defining <a>custom elements</a>,
	one often wants to set up "default" styles for them,
	akin to the user-agent styles that apply to built-in elements.
	This is, unfortunately, hard to do in vanilla CSS,
	due to issues of scoping and specificity--
	the element in question might be used in <a>shadow trees</a>,
	and thus is unreachable by any selector targeting it in the outermost document;
	and selectors, even low-specificity ones like simple <a>type selectors</a>,
	can accidentally override author-level styles meant to target the element.

	To aid in this, this section defines a way
	to create a stylesheet of "default element styles" for a given element.
	This stylesheet applies across the entire document,
	in all <a>shadow trees</a>,
	and the rules in it apply at the <a>user-agent origin</a>,
	so author-level rules automatically win.

	{{Window}}s gain a private slot <dfn attribute for=Window>\[[defaultElementStylesMap]]</dfn>
	which is a map of <a for="Element">local names</a> to <a>stylesheets</a>.

	These stylesheets must apply to every document in the window.
	They must be interpreted as user agent stylesheets.

	Note: This implies, in particular,
	that they apply to all <a>shadow trees</a> in every document,
	and that the declarations in them are from the <a>user-agent origin</a>.

	For the purpose of the <a>cascade</a>,
	these stylesheets are ordered after the user agent's own stylesheets;
	their relative ordering doesn't matter as it is not observable.

	Within these stylesheets, <a>complex selectors</a> must be treated as invalid.
	Every <a>compound selector</a> must be treated as containing an additional <a>type selector</a>
	that selects elements with the <a for=Element>local name</a>
	that the stylesheet is keyed with.

	Issue: Do we need to restrict the <a>at-rules</a> that can be used in these sheets?
	For example, do we allow an ''@font-face''?
	I'm going to leave it as allowed unless/until I hear complaints.

	This specification does not define how to add to, remove from, or generally manipulate
	the {{[[defaultElementStylesMap]]}}.
	It is expected that other specifications,
	such as [[DOM]],
	will define ways to do so.

<!--
 ██████  ██     ██    ███    ████████   ███████  ██      ██
██    ██ ██     ██   ██ ██   ██     ██ ██     ██ ██  ██  ██
██       ██     ██  ██   ██  ██     ██ ██     ██ ██  ██  ██
 ██████  █████████ ██     ██ ██     ██ ██     ██ ██  ██  ██
      ██ ██     ██ █████████ ██     ██ ██     ██ ██  ██  ██
██    ██ ██     ██ ██     ██ ██     ██ ██     ██ ██  ██  ██
 ██████  ██     ██ ██     ██ ████████   ███████   ███  ███
-->

<h2 id='shadow-dom'>
Shadow Encapsulation</h2>

<h3 id='shadow-gloss'>
Informative Explanation of Shadow DOM</h3>

	<em>The following is a non-normative explanation
	of several concepts normatively defined in the DOM Standard [[!DOM]],
	to aid in understanding what this spec defines
	without having to fully grok the DOM Standard.</em>

	In addition to the qualities of an element tree defined in [[SELECTORS4#data-model]],
	the DOM Standard adds several new concepts related to <a>shadow trees</a>,
	several of which are relevant to CSS.

	An element can host a <a>shadow tree</a>,
	which is a special kind of document fragment
	with a <a>shadow root</a>
	(a non-element node)
	at its root.
	Children of the <a>shadow root</a> are ordinary elements and other nodes.
	The element hosting the <a>shadow tree</a> is its <a>host</a>,
	or <a>shadow host</a>.

	The elements in a <a>shadow tree</a> are not <a>descendants</a> of the <a>shadow host</a> in general
	(including for the purposes of Selectors like the <a>descendant combinator</a>).
	However, the <a>shadow tree</a>,
	when it exists,
	is used in the construction of the <a>flattened element tree</a>,
	which CSS uses for all purposes <em>after</em> Selectors
	(including inheritance and box construction).

	Loosely, the <a>shadow tree</a> is treated as the <a>shadow host's</a> contents
	instead of its normal <a>light tree</a> contents.
	However, some of its <a>light tree</a> children
	can be "pulled into" the <a>shadow tree</a>
	by assigning them to <a>slots</a>.
	This causes them to be treated as children of the <a>slot</a>
	for CSS purposes.
	The <a>slots</a> can then be assigned to <a>slots</a>
	in deeper <a>shadow trees</a>;
	luckily, <a>slots</a> themselves don't generate boxes by default,
	so you don't get an unpredictable cascade of <{slot}> wrapper elements
	disrupting your CSS.

	If nothing is explicitly assigned to a <a>slot</a>,
	the <a>slot's</a> own children are instead assigned to it,
	as a sort of "default" contents.

<h3 id='selectors'>
Shadow DOM and Selectors</h3>

<h4 id='selectors-data-model'>
Matching Selectors Against Shadow Trees</h4>

	When a selector is matched against a <a>shadow tree</a>,
	the selector match list
	is initially the <a>shadow host</a>,
	followed by all children of the <a>shadow tree's</a> <a>shadow root</a>
	and their descendants,
	ordered by a pre-order traversal.

	Issue: Rewrite this against the newer call forms of the matching algorithms.

	Note: Remember that the <a>descendants</a> of an element
	are based on the <a>light tree</a> <a>children</a> of the element,
	which does not include the <a>shadow trees</a> of the element.

	When a selector is <a lt="match a selector against a tree">matched against a tree</a>,
	its <dfn export>tree context</dfn> is the <a>root</a> of the <var>root elements</var> passed to the algorithm.
	If the <a>tree context</a> is a <a>shadow root</a>,
	that selector is being matched <dfn export lt="in the context of a shadow tree|in the context of the shadow tree">in the context of a shadow tree</dfn>.

	<div class="example">
		For example,
		any selector in a stylesheet embedded in or linked from an element in a <a>shadow tree</a>
		is <a>in the context of a shadow tree</a>.
		So is the argument to {{querySelector()}} when called from a <a>shadow root</a>.
	</div>

	Declarations inherit the <a>tree context</a>
	of the selector that was matched to apply them.

<wpt>
	css-scoping-shadow-assigned-node-with-before-after.html
	css-scoping-shadow-dynamic-remove-style-detached.html
	css-scoping-shadow-assigned-node-with-rules.html
	css-scoping-shadow-host-with-before-after.html
	css-scoping-shadow-invisible-slot.html
	css-scoping-shadow-root-hides-children.html
	css-scoping-shadow-slot-display-override.html
	css-scoping-shadow-slot-fallback.html
	css-scoping-shadow-slot.html
	css-scoping-shadow-slot-style.html
	css-scoping-shadow-with-outside-rules.html
	css-scoping-shadow-with-rules.html
	css-scoping-shadow-with-rules-no-style-leak.html
	keyframes-001.html
	keyframes-002.html
	shadow-assign-dynamic-001.html
	shadow-disabled-sheet-001.html
	shadow-fallback-dynamic-001.html
	shadow-fallback-dynamic-002.html
	shadow-fallback-dynamic-003.html
	shadow-fallback-dynamic-004.html
	shadow-fallback-dynamic-005.html
	shadow-host-with-before-after.html
	shadow-link-rel-stylesheet-no-style-leak.html
	shadow-multiple-links.html
	shadow-reassign-dynamic-001.html
	shadow-reassign-dynamic-002.html
	shadow-reassign-dynamic-004.html
	shadow-root-insert-into-document.html
	whitespace-crash-001.html
</wpt>

<h4 id='host-element-in-tree'>
Selecting Shadow Hosts from within a Shadow Tree</h4>

	A <a>shadow host</a> is outside of the <a>shadow tree</a> it hosts,
	and so would ordinarily be untargettable by any selectors evaluated <a>in the context of the shadow tree</a>
	(as selectors are limited to a single tree),
	but it is sometimes useful to be able to style it from inside the <a>shadow tree</a> context.

	For the purpose of Selectors,
	a <a>shadow host</a> also appears in its <a>shadow tree</a>,
	with the contents of the <a>shadow tree</a> treated as its children.
	(In other words, the <a>shadow host</a> is treated as replacing the <a>shadow root</a> node.)

	When considered within its own <a>shadow trees</a>,
	the <a>shadow host</a> is <a>featureless</a>.
	Only the '':host'', '':host()'', and '':host-context()'' pseudo-classes are allowed to match it.

	<details class='note'>
		<summary>Why is the shadow host so weird?</summary>

		The <a>shadow host</a> lives outside the <a>shadow tree</a>,
		and its markup is in control of the page author,
		not the component author.

		It would not be very good if a component used a particular class name
		internally in a <a>shadow tree</a> stylesheet,
		and the page author using the component accidentally <em>also</em>
		used the same class name and put it on the <a>shadow host</a>.
		Such a situation would result in accidental styling
		that is impossible for the component author to predict,
		and confusing for the page author to debug.

		However, there are still some reasonable use-cases for letting a stylesheet in a <a>shadow tree</a>
		style its <a>shadow host</a>.
		(For example,
		the component might want to be laid out as a flexbox,
		requiring the <a>shadow host</a> to be set to ''display: flex''.)
		So, to allow this situation but prevent accidental styling,
		the <a>shadow host</a> appears but is completely featureless
		and unselectable except through '':host'' and its related functional forms,
		which make it very explicit when you're trying to match against markup provided by the page author.
	</details>

<wpt>
	host-with-default-namespace-001.html
	host-context-parsing.html
	host-defined.html
	host-has-001.html
	host-has-002.html
	host-has-003.html
	host-is-001.html
	host-is-002.html
	host-is-003.html
	host-is-004.html
	host-is-005.html
	host-parsing.html
</wpt>

<!--
 ██  ██     ██  ███████   ██████  ████████
████ ██     ██ ██     ██ ██    ██    ██
 ██  ██     ██ ██     ██ ██          ██
     █████████ ██     ██  ██████     ██
 ██  ██     ██ ██     ██       ██    ██
████ ██     ██ ██     ██ ██    ██    ██
 ██  ██     ██  ███████   ██████     ██
-->

<h4 id='host-selector'>
Selecting Into the Light: the '':host'', '':host()'', and '':host-context()'' pseudo-classes</h4>

	The <dfn selector>:host</dfn> pseudo-class,
	when evaluated <a>in the context of a shadow tree</a>,
	matches the <a>shadow tree's</a> <a>shadow host</a>.
	In any other context,
	it matches nothing.

	The <dfn selector id="selectordef-host-function">:host()</dfn> function pseudo-class
	has the syntax:

	<pre>:host( <<compound-selector>> )</pre>

	When evaluated <a>in the context of a shadow tree</a>,
	it matches the <a>shadow tree's</a> <a>shadow host</a>
	if the <a>shadow host</a>,
	in its normal context,
	matches the selector argument.
	In any other context,
	it matches nothing.

	The [=specificity=] of '':host'' is that of a pseudo-class.
	The [=specificity=] of '':host()'' is that of a pseudo-class,
	plus the [=specificity=] of its argument.

	Note: This is different from the specificity of similar pseudo-classes,
	like '':is()'' or '':not()'',
	which <em>only</em> take the specificity of their argument.
	This is because '':host'' is affirmatively selecting an element all by itself,
	like a "normal" pseudo-class;
	it takes a selector argument for syntactic reasons
	(we can't say that '':host.foo'' matches but ''.foo'' doesn't),
	but is otherwise identical
	to just using '':host'' followed by a selector.

	<div class='example'>
		For example, say you had a component with a <a>shadow tree</a> like the following:

		<pre>
			&lt;x-foo class="foo">
				&lt;"shadow tree">
					&lt;div class="foo">...&lt;/div>
				&lt;/>
			&lt;/x-foo>
		</pre>

		For a stylesheet within the <a>shadow tree</a>:

		* '':host'' matches the <code>&lt;x-foo></code> element.
		* ''x-foo'' matches nothing.
		* ''.foo'' matches only the <code>&lt;div></code> element.
		* ''.foo:host'' matches nothing
		* '':host(.foo)'' matches the <code>&lt;x-foo></code> element.
	</div>

	Ordinary, selectors within a <a>shadow tree</a>
	can't see elements outside the <a>shadow tree</a> at all.
	Sometimes, however, it's useful to select an ancestor that lies somewhere outside the shadow tree,
	above it in the document.

	<div class='example'>
		For example, a group of components can define a handful of color themes
		they know how to respond to.
		Page authors could opt into a particular theme
		by adding a specific class to the components,
		or higher up in the document.
	</div>

	The <dfn selector>:host-context()</dfn> functional pseudo-class tests whether there is an ancestor,
	outside the <a>shadow tree</a>,
	which matches a particular selector.
	Its syntax is:

	<pre class=prod>:host-context( <<compound-selector>> )</pre>

	When evaluated <a>in the context of a shadow tree</a>,
	the '':host-context()'' pseudo-class matches the <a>shadow host</a>,
	if the <a>shadow host</a> or one of its <a>shadow-including ancestors</a>
	matches the provided <<compound-selector>>.
	In any other context,
	it matches nothing.

	The [=specificity=] of '':host-context()'' is that of a pseudo-class,
	plus the [=specificity=] of its argument.

	Note: This means that the selector pierces through shadow boundaries on the way up,
	looking for elements that match its argument,
	until it reaches the document root.

<wpt>
	css-scoping-shadow-host-functional-rule.html
	css-scoping-shadow-host-namespace.html
	css-scoping-shadow-host-rule.html
	host-context-specificity-001.html
	host-context-specificity-002.html
	host-context-specificity-003.html
	host-descendant-001.html
	host-descendant-002.html
	host-descendant-invalidation.html
	host-dom-001.html
	host-functional-descendant-invalidation.html
	host-multiple-001.html
	host-nested-001.html
	host-slotted-001.html
	host-specificity-002.html
	host-specificity-003.html
	host-specificity.html
	shadow-host-removal-invalidation.html
	shadow-at-import.html
	shadow-link-rel-stylesheet.html
	shadow-shared-style-cache-001.html
	stylesheet-title-001.html
	stylesheet-title-002.html
</wpt>


<!--
 ██   ██   ██████  ██        ███████  ████████ ████████ ████████ ████████
████ ████ ██    ██ ██       ██     ██    ██       ██    ██       ██     ██
 ██   ██  ██       ██       ██     ██    ██       ██    ██       ██     ██
           ██████  ██       ██     ██    ██       ██    ██████   ██     ██
 ██   ██        ██ ██       ██     ██    ██       ██    ██       ██     ██
████ ████ ██    ██ ██       ██     ██    ██       ██    ██       ██     ██
 ██   ██   ██████  ████████  ███████     ██       ██    ████████ ████████
-->

<h4 id='slotted-pseudo'>
Selecting Slot-Assigned Content: the ''::slotted()'' pseudo-element</h4>

	The <dfn selector>::slotted()</dfn> pseudo-element
	represents the elements <a lt="find flattened slottables">assigned, after flattening,</a> to a <a>slot</a>.
	This pseudo-element only exists on <a>slots</a>.

	The ''::slotted()'' pseudo-element is an <em>alias</em> for other elements in the tree,
	and does not generate any boxes itself.

	The grammar of the ''::slotted()'' pseudo-element is:

	<pre class=prod>::slotted( <<compound-selector>> )</pre>

	The ''::slotted()'' pseudo-element represents the elements that are:

	* <a lt="find flattened slottables">assigned, after flattening,</a> to the <a>slot</a> that is ''::slotted''’s originating element
	* <a lt="match a selector against an element">matched</a> by its <<compound-selector>> argument

	The ''::slotted()'' pseudo-element can be followed by a <a>tree-abiding pseudo-element</a>,
	like ''::slotted()::before'',
	representing the appropriate pseudo-element of the elements represented by the ''::slotted()'' pseudo-element.

	The [=specificity=] of ''::slotted()'' is that of a pseudo-element,
	plus the [=specificity=] of its argument.

	<div class="example">
		For example, say you had a component with both children and a shadow tree,
		like the following:

		<pre>
			&lt;x-foo>
				&lt;div id="one" slot="foo" class="foo">...&lt;/div>
				&lt;div id="two" slot="foo">...&lt;/div>
				&lt;div id="three" class="foo">
					&lt;div id="four" slot="foo">...&lt;/div>
				&lt;/div>
				&lt;"shadow tree">
					&lt;div id="five">...&lt;/div>
					&lt;div id="six">...&lt;/div>
					&lt;slot name="foo">&lt;/slot>
				&lt;/"shadow tree">
			&lt;/x-foo>
		</pre>

		For a stylesheet within the <a>shadow tree</a>,
		a selector like ''::slotted(*)''
		selects ''#one'' and ''#two'' only,
		as they're the elements <a lt="find flattened slottables">assigned</a> to the sole <{slot}> element.
		It will <em>not</em> select ''#three''
		(no <code>slot</code> attribute)
		nor ''#four''
		(only direct <a>children</a> of a <a>shadow host</a> can be assigned to a <a>slot</a>).

		A selector like ''::slotted(.foo)'',
		on the other hand,
		will only select ''#one'',
		as it matches ''.foo'',
		but ''#two'' doesn't.

		Note: Note that a selector like ''::slotted(*)''
		is equivalent to ''*::slotted(*)'',
		where the ''*'' selects many more elements than just the <{slot}> element.
		However, since only the <{slot}> elements are <a>slots</a>,
		they're the only elements with a ''::slotted()'' pseudo-element as well.
	</div>

	Note: ''::slotted()'' can only represent the <em>elements</em>
	assigned to the <a>slot</a>.
	<a>Slots</a> can also be assigned text nodes,
	which can't be selected by ''::slotted()''.
	The only way to style assigned text nodes
	is by styling the <a>slot</a> and relying on inheritance.

<h4 id='the-has-slotted-pseudo'>
Matching on the Presence of Slot-Assigned Nodes: the '':has-slotted'' pseudo-class</h4>

	The <dfn selector>:has-slotted</dfn> pseudo-class
	matches <{slot}> elements
	which have a non-empty list of <a lt="find flattened slottables">flattened slotted nodes</a>.

	When '':has-slotted'' matches a slot with fallback content,
	we can conclude that the fallback content is <em>not</em> being displayed.

	Note: Even a single whitespace text node is sufficient to make '':has-slotted''' apply.
	This is by design, so that the behavior of this pseudo-class is consistent with the behavior of the {{HTMLSlotElement/assignedNodes()}} method.
	A future version of this specification is expected to introduce a way to exclude this case from matching.

	Note: It is expected that a future version of this specification will introduce a functional '':has-slotted()'' pseudo-class that allows
	more fine-grained matching by accepting a selector argument.
	'':has-slotted'' is <em>not</em> an alias of '':has-slotted(*)'',
	as the latter would not match slotted text nodes, but '':has-slotted'' does.

<wpt>
	css-scoping-shadow-slotted-nested.html
	css-scoping-shadow-slotted-rule.html
	reslot-text-inheritance.html
	slotted-invalidation.html
	slotted-link.html
	slotted-matches.html
	slotted-nested.html
	slotted-parsing.html
	slotted-placeholder.html
	slotted-slot.html
	slotted-specificity-002.html
	slotted-specificity.html
	slotted-with-pseudo-element.html
</wpt>

<!--
 ██████     ███     ██████   ██████     ███    ████████  ████████
██    ██   ██ ██   ██    ██ ██    ██   ██ ██   ██     ██ ██
██        ██   ██  ██       ██        ██   ██  ██     ██ ██
██       ██     ██  ██████  ██       ██     ██ ██     ██ ██████
██       █████████       ██ ██       █████████ ██     ██ ██
██    ██ ██     ██ ██    ██ ██    ██ ██     ██ ██     ██ ██
 ██████  ██     ██  ██████   ██████  ██     ██ ████████  ████████
-->

<h3 id='shadow-cascading'>
Shadow Trees and the Cascade</h3>

	See [[css-cascade-4#cascade-sort]].

<wpt>
	shadow-cascade-order-001.html
</wpt>

<!--
████████ ██          ███    ████████       ████████ ████████  ████████ ████████
██       ██         ██ ██      ██             ██    ██     ██ ██       ██
██       ██        ██   ██     ██             ██    ██     ██ ██       ██
██████   ██       ██     ██    ██             ██    ████████  ██████   ██████
██       ██       █████████    ██             ██    ██   ██   ██       ██
██       ██       ██     ██    ██             ██    ██    ██  ██       ██
██       ████████ ██     ██    ██             ██    ██     ██ ████████ ████████
-->

<h3 id='flattening' algorithm>
Flattening the DOM into an Element Tree</h3>

	While Selectors operates on the DOM tree as the host language presents it,
	with separate trees that are unreachable via the standard parent/<a>child</a> relationship,
	the rest of CSS needs a single unified tree structure to work with.
	This is called the <dfn export lt="flat tree|flattened element tree">flattened element tree</dfn>
	(or <a>flat tree</a>),
	and is constructed as follows:

	1. Let <var>pending nodes</var> be a list of DOM nodes with associated parents,
		initially containing just the document's <a>root</a> element with no associated parent.
	2. Repeatedly execute the following substeps until <var>pending nodes</var> is empty:
		1. Pop the first element from <var>pending nodes</var>,
			and assign it to <var>pending node</var>.
		2. Insert <var>pending node</var> into the <a>flat tree</a>
			as a child of its associated parent.
			(If it has no associated parent,
			it's the document root--
			just insert it into the <a>flat tree</a> as its root.)
		3. Perform one of the following,
			whichever is the first that matches:

			<dl class=switch>
				<dt><var>pending node</var> is a <a>shadow host</a>
				<dd>
					Append the child nodes of the <a>shadow root</a> of the <a>shadow tree</a> it hosts
					to <var>pending nodes</var>,
					with <var>pending node</var> as their associated parent.
				<dt><var>pending node</var> is a <a>slot</a>
				<dd>
					<a>Find slottables</a> for <var>pending node</var>,
					and append them to <var>pending nodes</var>,
					with <var>pending node</var> as their associated parent.

					If no <a>slottables</a> were found for <var>pending node</var>,
					instead append its <a>children</a> to <var>pending nodes</var>,
					with <var>pending node</var> as their associated parent.
				<dt>Otherwise,
				<dd>
					Append the child nodes of <var>pending node</var>’s <a>light tree</a>
					to <var>pending nodes</var>,
					with <var>pending node</var> as their associated parent.
			</dl>

	Note: In other words, the <a>flat tree</a> is the top-level DOM tree,
	but <a>shadow hosts</a> are filled with their <a>shadow tree</a> children
	instead of their <a>light tree</a> children
	(and this proceeds recursively if the <a>shadow tree</a> contains any <a>shadow hosts</a>),
	and <a>slots</a> get filled with the nodes that are assigned to them
	(and this proceeds recursively if the <a>slots</a> are themselves
	assigned to a <a>slot</a> in a deeper <a>shadow tree</a>).

	Issue: A non-obvious result of this
	is that elements assigned to a slot inherit from that slot,
	not their light-tree parent
	or any deeper slots their slot gets assigned to.
	This means that text nodes are styled by the shadow tree of their parent,
	with nobody else capable of intervening in any way.
	Do we want an additional pseudo-element for targeting those text nodes
	so they can be styled at all slot-assignment levels,
	like normal elements can be?
	This implies it needs to work for text nodes in the light tree
	before they're assigned downwards,
	so this can't just be a ''::slotted()'' variant.
	Luckily, this is a long-standing request!

<h4 id='slots-in-shadow-tree'>
Slots and Slotted Elements in a Shadow Tree</h4>

	<a>Slots</a> must act as if they were assigned ''display: contents''
	via a rule in the <a>UA origin</a>.
	This must be possible to override via 'display',
	so they <em>do</em> generate boxes if desired.

	Note: A non-obvious result of assigning elements to <a>slots</a>
	is that they <a>inherit</a> from the <a>slot</a> they're assigned to.
	Their original <a>light tree</a> parent,
	and any deeper <a>slots</a> that their <a>slot</a> gets assigned to,
	don't affect inheritance.

<wpt>
	css-scoping-shadow-nested-slot-display-override.html
	shadow-reassign-dynamic-003.html
	slot-non-html-display-value.html
</wpt>

<!--
████████ ████████  ████████ ████████          ██████   ██████   ███████  ████████  ████████ ████████
   ██    ██     ██ ██       ██               ██    ██ ██    ██ ██     ██ ██     ██ ██       ██     ██
   ██    ██     ██ ██       ██               ██       ██       ██     ██ ██     ██ ██       ██     ██
   ██    ████████  ██████   ██████   ███████  ██████  ██       ██     ██ ████████  ██████   ██     ██
   ██    ██   ██   ██       ██                     ██ ██       ██     ██ ██        ██       ██     ██
   ██    ██    ██  ██       ██               ██    ██ ██    ██ ██     ██ ██        ██       ██     ██
   ██    ██     ██ ████████ ████████          ██████   ██████   ███████  ██        ████████ ████████
-->

<h3 id='shadow-names'>
Name-Defining Constructs and Inheritance</h3>

	[=Shadow trees=] are meant to be an encapsulation boundary,
	allowing independent authors to share code
	without accidentally polluting each other's namespaces.
	For example, element IDs,
	which are generally meant to be unique within a document,
	can be validly used multiple times
	as long as each use is in a different [=shadow tree=].

	Similarly, several [=at-rules=] in CSS,
	such as ''@keyframes'' or ''@font-face'',
	define a name that later at-rules or properties can refer to them by.
	Like IDs, these names are globally exposed and unique within a document;
	also like IDs,
	this restriction is now loosened to being unique within a given [=shadow tree=].

	However, property inheritance can carry values from one tree to another,
	which complicates referencing the correct definition of a given name.
	Done naively, this can produce surprising and confusing results for authors.
	This section defines a set of concepts to use in defining and referencing "global" names
	in a way that respects encapsulation
	and doesn't give surprising results.

	If an at-rule or property defines a name that other CSS constructs can refer to it by,
	such as a ''@font-face'' '@font-face/font-family!!descriptor' name
	or an 'anchor-scope' name,
	it must be defined as a <dfn export for=CSS>tree-scoped name</dfn>.
	[=Tree-scoped names=] are associated with
	the [=root=] of the [=element=] hosting the stylesheet
	that the at-rule or property is declared in.

	Additionally, [=tree-scoped names=] can be <dfn export for="tree-scoped name">loosely matched</dfn> or <dfn export for="tree-scoped name">strictly matched</dfn>,
	defaulting to [=loosely matched=] unless otherwise specified.
	A [=loosely matched=] [=tree-scoped name=]
	can be matched by [=tree-scoped references=]
	(see below)
	in the same tree or descendant trees,
	while a [=strictly matched=] [=tree-scoped name=]
	can only be matched by [=tree-scoped references=]
	in the exact same tree.

	Properties or descriptors that reference a [=tree-scoped name=],
	such as the 'font-family!!property'
	or 'animation-name' properties,
	must define their value as a <dfn export for=CSS>tree-scoped reference</dfn>.
	These references implicitly capture
	a [=node tree=] [=root=]
	along with their specified value:
	unless otherwise specified,
	the [=root=] of the [=element=] hosting the stylesheet that the property or descriptor is declared in.
	This [=root=] reference stays with the [=tree-scoped reference=]
	as it is inherited.

	If a [=tree-scoped name=] is <dfn export for="tree-scoped name">global</dfn>
	(such as ''@font-face'' names),
	then when a [=tree-scoped reference=] is dereferenced to find it,
	first search only the [=tree-scoped names=] associated with the same [=root=]
	as the [=tree-scoped reference=].
	If no relevant [=tree-scoped name=] is found,
	and the [=root=] is a [=shadow root=],
	then repeat this search in the [=root=]'s [=host=]'s [=node tree=] (recursively).
	(In other words, global [=tree-scoped names=] “inherit” into descendant shadow trees,
	so long as they don't define the same name themselves.)

	If a [=tree-scoped name=] is <dfn export for="tree-scoped name">local</dfn> to an element
	(such as 'anchor-name' or 'anchor-scope' values),
	then whether a [=tree-scoped reference=] matches the [=tree-scoped name=] on a given element
	depends on whether the [=tree-scoped name=] is [=tree-scoped name/strictly matched|strictly=] or [=tree-scoped name/loosely matched|loosely=] matched.
	A [=tree-scoped name/strictly matched=] [=tree-scoped name=] only matches if
	both names are associated with the same tree.
	A [=tree-scoped name/loosely matched=] [=tree-scoped name=] also matches if
	the [=tree-scoped name=] is associated with an ancestor tree.

	If two [=tree-scoped names=] are directly compared
	(for example, when comparing [=computed values=]),
	they are considered to match only if their identifiers match,
	<em>and</em> their [=root=]s match exactly.
	(If one has a [=root=] that's an ancestor of the other, for example,
	they <em>do not</em> match.)

	<div class=issue>
		TODO: Fix all the at-rules that define global names,
		and the properties that reference them,
		to use these concepts.

		Global names include:

		* ''@font-face'', referenced by 'font-family!!property'
		* ''@font-feature-values'', referenced by 'font-family!!property'
		* ''@keyframes'', referenced by 'animation-name'
		* ''@counter-style'', referenced by 'list-style-type'
		* ''@color-profile'', referenced by the ''color()'' function
		* ''@font-palette-values'', referenced by 'font-palette'
		* others?
	</div>

	<div class=example>

		For example,
		given the following document
		(using the imaginary &lt;::shadow>&lt;/::shadow> markup
		to indicate an element's [=shadow tree=]):

		<xmp highlight=markup>
			<p class=outer>Here's some text in the outer document's "foo" font.
			<style>
				@font-face {
					font-family: foo;
					src: url(https://example.com/outer.woff);
				}
				body { font-family: foo; }
				my-component::part(text) { font-family: foo; }
			</style>

			<my-component>
				<::shadow>
					<p class=inner-default>I'm inheriting the outer document's font-family.
					<p class=inner-styled>And I'm explicitly styled to be in the component's "foo" font.
					<p class=part-styled part=text>
						I'm explicitly styled by the outer document,
						and get the outer document's "foo" font.
					<style>
						@font-face {
							font-family: foo;
							src: url(https://example.com/inner.woff);
						}
						.inner-styled { font-family: foo; }
					</style>
				</::shadow>
			</my-component>
		</xmp>

		The ''.outer'' element references the outer ''@font-face'', using the "outer.woff" file.

		The ''.inner-default'' element inherits the ''font-family: foo'' value from the outer document,
		using the same [=tree-scoped reference=] as ''.outer'',
		and thus also uses the "outer.woff" font file.

		The ''.inner-style'' element, on the other hand,
		receives a ''font-family: foo'' from the stylesheet inside the shadow,
		and thus its [=tree-scoped reference=] refers to the shadow's ''@font-family'',
		and it uses the "inner.woff" file.

		The ''.part-styled'' element also receives its style from the outer document,
		tho by being directly set rather than by inheritance.
		Thus, its [=tree-scoped reference=] also refer's to the outer document,
		and it uses the "outer.woff" file.
	</div>

	<div class=example>
		Here is a more complex example,
		showing three levels of trees,
		and illustrating precisely how [=tree-scoped names=] and [=tree-scoped references=] inherit.

		<xmp highlight=markup>
			<style>
				@font-face {
					font-family: foo;
					src: url(https://example.com/outer.woff);
				}
				body { font-family: foo; }
			</style>

			<child-component>
				<::shadow>
					<style>
						@font-face {
							font-family: foo;
							src: url(https://example.com/inner.woff);
						}
					</style>

					<grandchild-component>
						<::shadow>
							<p class=inner-default>
								I'm inheriting the outer document's "foo" font.
							</p>
							<p class=inner-search>
								And I can't find a local "foo" font,
								so I'm searching further up the tree,
								and find the shadow's "foo" font.
							</p>
							<style>
							.inner-search { font-family: foo; }
							</style>
						</::shadow>
					</grandchild-component>
				</::shadow>
			</child-component>
		</xmp>

		Here, just as in the previous example,
		''.inner-default'' is inheriting the ''font-family: foo'' declared in the outer document,
		and so it ends up referencing the outer document's ''@font-face'',
		and is rendered with the "outer.woff" file.

		On the other hand,
		''.inner-search'' receives its style from a stylesheet in <code>&lt;grandchild-component></code>'s shadow tree,
		so it attempts to find a ''@font-face'' defining a ''foo'' font in that tree.
		There is no such ''@font-face'',
		so it starts walking up the shadow trees,
		finding an appropriate ''@font-face'' in <code>&lt;child-component></code>,
		so it's rendered with the "inner.woff" file.
	</div>

<wpt>
	font-face-001.html
	font-face-002.html
	font-face-003.html
	font-face-004.html
	font-face-005.html
	font-face-006.html
	font-face-007.html
	font-face-008.html
	font-face-009.html
	keyframes-003.html
	keyframes-004.html
	keyframes-005.html
	keyframes-006.html
	scoped-reference-animation-001.html
	scoped-reference-animation-002.html
</wpt>

<h4 id='shadow-names-serialization'>
Serialized Tree-Scoped References</h4>

	If a [=tree-scoped reference=] is serialized,
	it serializes only its value;
	the associated [=root=] is lost.

	<div class=example>

		This implies that `el.style.foo = getComputedStyle(el).foo;`
		is not necessarily a no-op,
		like it typically was before [=shadow trees=] existed.

		For example,
		given the following document
		(using the imaginary &lt;::shadow>&lt;/::shadow> markup
		to indicate an element's [=shadow tree=]):

		<xmp highlight=markup>
			<p class=outer>Here's some text in the outer document's "foo" font.
			<style>
				@font-face {
					font-family: foo;
					src: url(foo.woff);
				}
				body { font-family: foo; }
			</style>

			<my-component>
				<::shadow>
					<p class=inner-default>I'm inheriting the outer document's font-family.
					<p class=inner-styled>And I'm explicitly styled to be in the component's "foo" font.
					<style>
						@font-face {
							font-family: foo;
							src: url(https://example.com/foo.woff);
						}
						.inner-styled { font-family: foo; }
					</style>
					<script>
						const innerDefault = document.querySelector('.inner-default');
						const innerStyled = document.querySelector('.inner-styled');
						const defaultFont = getComputedStyle(innerDefault).fontFamily;
						const styledFont = getComputedStyle(innerStyled).fontFamily;

						console.log(defaultFont == styledFont); // true!
					</script>
				</::shadow>
			</my-component>
		</xmp>

		The <code>.outer</code> element is styled with the outer document's "foo" ''@font-face''.
		The <code>.inner-default</code> element inherits 'font-family' from the outer document,
		meaning it inherits a [=tree-scoped reference=]
		referencing that outer document,
		and so it's in the same font as <code>.outer</code>.

		Meanwhile, <code>.inner-styled</code> is explicitly styled from inside the shadow root,
		so it receives a fresh [=tree-scoped reference=]
		referencing its shadow tree,
		and it is instead styled the shadow's own "foo" ''@font-face''.

		Despite that, the script running inside the component
		sees the two elements as having the same value for 'font-family',
		because the [=root=]-reference part of a [=tree-scoped reference=]
		is not preserved by serialization.
		If it were to set <code highlight=js>innerDefault.style.fontFamily = defaultFont;</code>
		(thus setting the 'font-family' property of the element's attribute stylesheet,
		which lives in the shadow tree),
		the <code>.inner-default</code> element would suddenly switch
		to the same font as <code>.inner-styled</code>!
	</div>

	Note: The [[css-typed-om-1]] is expected to reflect the [=root=] reference of a [=tree-scoped reference=]
	in its [=reification=] rules for values,
	allowing authors to tell what [=node tree=] the reference is taking its values from,
	and allowing values to be transported across [=node trees=]
	without changing their meaning.


<h2 id="changes">
Changes</h2>

	The following significant changes were made since the
	<a href="https://www.w3.org/TR/2014/WD-css-scoping-1-20140403/">3 April 2014 Working Draft</a>.

	* Defined that tree-scoped names inherit into descendant shadow trees
	* Renamed <css>::content</css> to ''::slotted''.
	* Define the flattened tree.
	* Generally reorg and rebase the Shadow DOM section on top of current DOM.
	* Punt <css>@scope</css> and related things,
		and <css>::region</css> and related things,
		to the next level of the draft.
	* Define the specificity of '':host'', '':host()'', '':host-context()'', and ''::slotted()''
	* Remove the <css>>>></css> (previously called <css>/deep/</css>) combinator.
	* Define that tree-abiding pseudos are allowed after ''::slotted()''.
	* Allow <<compound-selector-list>> in all the pseudos.
	* Define a way to create a stylesheet of default element styles for a given element.
	* Make featureless elements match nothing.
	* Define <a>in the context of a shadow tree</a>.

Privacy Considerations {#privacy}
===============================================

This specification introduces Shadow DOM and some shadow-piercing capabilities,
but this does not introduce any privacy issues--
shadow DOM, as currently specified, is intentionally not a privacy boundary
(and the parts of the UA that use shadow DOM and <em>do</em> have a privacy boundary
implicitly rely on protections not yet specified,
which protect them from the things defined in this specification).

Security Considerations {#security}
===============================================

This specification introduces Shadow DOM and some shadow-piercing capabilities,
but this does not introduce any security issues--
shadow DOM, as currently specified, is intentionally not a security boundary
(and the parts of the UA that use shadow DOM and <em>do</em> have a security boundary
implicitly rely on protections not yet specified,
which protect them from the things defined in this specification).

<wpt hidden>
	chrome-1492368-crash.html
</wpt>
